﻿/*
 * Created by SharpDevelop.
 * User: lebeda
 * Date: 28.2.2014
 * Time: 8:30
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace simpleExplorer.menuProviderImpl
{
    /// <summary>
    /// Description of FilesystemRecursiveMenuProvider.
    /// </summary>
    public class FilesystemRecursiveMenuProvider: AbstractMenuProvider, MenuItemProvider
    {
        
        private static int MAX_MENU = 20; // maximum of display menus
        
        Dictionary<string, int> execlog;
        
        public FilesystemRecursiveMenuProvider(MenuItem aMenuItem)
        {
            this.SelfMenuItem = aMenuItem;
            string path = Regex.Replace(aMenuItem.cmd, @"\\$", "");
            
            //  buildCustomMenus(path);
            buildFilesMenu(path);
            
            execlog = ExecLog.load();
        }
        
        //        static int CompateMenuItem(MenuItem item1, MenuItem item2) {
        //            int cmp = item1.count.CompareTo(item2.count);
        //            if (cmp == 0) {
        //                cmp = item1.caption.CompareTo(item2.caption);
        //            }
        //            return cmp;
        //        }
        
        private void buildFilesMenu(string path)
        {
            string[] filePaths = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            
            foreach (string file in filePaths) {
                string caption = Regex.Replace(file, ".*\\\\", "");
                caption = Regex.Replace(caption, ".lnk$", "", RegexOptions.IgnoreCase);
                MenuItem menuItem = new MenuItem(caption, ItemTarget.item, ItemType.execItem, file, "",
                                                 Path.GetDirectoryName(file), SelfMenuItem);
                menuIndex[menuItem.caption] = menuItem;
            }

            
            //            Comparison<MenuItem> comparison  = new Comparison<MenuItem>(CompateMenuItem);
            //            Array.Sort(filePaths, comparison );
            

        }
        
        public override List<MenuItem> GetMenuItemList(string filter)
        {
            List<MenuItem> result = new List<MenuItem>();
            
            foreach (MenuItem menuItem in menuIndex.Values) {
                string caption = menuItem.caption;
 
                menuItem.count = getCountExecution(caption);
                
                if (!String.IsNullOrWhiteSpace(filter)) {
                    string smallCaption = caption.ToLower();
                    string smallFind = filter.ToLower();
                    if (smallCaption.Contains(smallFind)) {
                        result.Add(menuItem);
                    }
                } else {
                    result.Add(menuItem);
                }
            }
            
            
            result.Sort((item1, item2) => {
                            int cmp = item2.count.CompareTo(item1.count);
                            if (cmp == 0) {
                                cmp = item1.caption.CompareTo(item2.caption);
                            }
                            return cmp;
                        });
            
            int end = (result.Count > MAX_MENU ? MAX_MENU : result.Count);
            
            return result.GetRange(0, end);
        }

        int getCountExecution(string caption)
        {
            int count = 0;

            if (execlog.ContainsKey(caption)) {
            	count = execlog[caption];
            }

			return count;
        }
        

    }
}
